#include <unistd.h>
#include <string.h>
#include "ups.h"
#include "zlog.h"
#include "gdsl.h"

#define NET_MODULE 77
#define MEDIA_MODULE 88

struct net_conf
{
	char ip[16];
	char mask[16];
	char gw[16];
	char dns1[16];
	char dns2[16];
	unsigned short port;
};

struct net_conf defnet = 
{
	.ip = "192.168.0.111",
	.mask = "255.255.240.0",
	.gw = "192.168.0.1",
	.dns1 = "192.168.0.1",
	.dns2 = "192.168.0.1",
	.port = 9300
};

struct media_conf 
{
    unsigned int  chn;          //通道号0~QV_MAX_CHN_NUM,如果为0xFF则为所有通道
    unsigned int  Stream;       //0-主码流,1-次码流,2-第三码流
    unsigned int  w;            //宽
    unsigned int  h;            //高
    unsigned int  FrameRate;    //帧率 (1-50/60) PAL为25，NTSC为30
    unsigned int  Standard;     //制式 (0为PAL制,1为NTSC制)
    unsigned int  RateType;     //流模式(0为定码流，1为变码流)
    unsigned int  dwBitRate;    //码率(8-16384),单位Kbps
    unsigned int  ImageQuality; //编码质量(0-5),0为最好,5为最差
    unsigned int  nMaxKeyInterval;//关键帧间隔(1-100)
};
struct media_conf def_venc = 
{
    .chn = 1,
    .Stream = 0, 
    .w = 1280,
    .h = 720, 
    .FrameRate = 25,
    .Standard = 0, 
    .RateType = 0, 
    .dwBitRate = 4096, 
    .ImageQuality = 1,
    .nMaxKeyInterval = 25
};

int net_get_func(void *conf, void *data,int size)
{
	struct net_conf *pconf = (struct net_conf *)conf;

	printf("\tups has get net conf:\n");
	printf("\t\tip = %s\n",pconf->ip);
	printf("\t\tmask = %s\n",pconf->mask);
	printf("\t\tgw = %s\n",pconf->gw);
	printf("\t\tdns1 = %s\n",pconf->dns1);
	printf("\t\tdns2 = %s\n",pconf->dns2);
	printf("\t\tport = %hu\n",pconf->port);
	return 0;
}
int net_set_func(void *conf, void *data,int size)
{
	struct net_conf *pconf = (struct net_conf *)conf;

	printf("\tups will set net conf:\n");
	printf("\t\tip = %s\n",pconf->ip);
	printf("\t\tmask = %s\n",pconf->mask);
	printf("\t\tgw = %s\n",pconf->gw);
	printf("\t\tdns1 = %s\n",pconf->dns1);
	printf("\t\tdns2 = %s\n",pconf->dns2);
	printf("\t\tport = %hu\n",pconf->port);
	return 0;
}

int media_get_func(void *conf, void *data,int size)
{
	struct media_conf *pconf = (struct media_conf *)conf;

	printf("\tups has get media conf:\n");
	printf("\t\tchn = %d\n",pconf->chn);
	printf("\t\tStream = %d\n",pconf->Stream);
	printf("\t\twidth = %d\n",pconf->w);
	printf("\t\theight = %d\n",pconf->h);
	printf("\t\tframerate = %d\n",pconf->FrameRate);
	printf("\t\tstandard = %d\n",pconf->Standard);
	printf("\t\tratetype = %d\n",pconf->RateType);
	printf("\t\tbitRate = %d\n",pconf->dwBitRate);
	printf("\t\tImageQuality = %d\n",pconf->ImageQuality);
	printf("\t\tMaxKeyInterval = %d\n",pconf->nMaxKeyInterval);
	return 0;
}
int media_set_func(void *conf, void *data,int size)
{
	struct media_conf *pconf = (struct media_conf *)conf;

	printf("\tups will set media conf:\n");
	printf("\t\tchn = %d\n",pconf->chn);
	printf("\t\tStream = %d\n",pconf->Stream);
	printf("\t\twidth = %d\n",pconf->w);
	printf("\t\theight = %d\n",pconf->h);
	printf("\t\tframerate = %d\n",pconf->FrameRate);
	printf("\t\tstandard = %d\n",pconf->Standard);
	printf("\t\tratetype = %d\n",pconf->RateType);
	printf("\t\tbitRate = %d\n",pconf->dwBitRate);
	printf("\t\tImageQuality = %d\n",pconf->ImageQuality);
	printf("\t\tMaxKeyInterval = %d\n",pconf->nMaxKeyInterval);
	return 0;
}

void *net_module_thread(void *arg)
{
	int ret = 0;
	zlog_category_t *zc_net = zlog_get_category("net");

	sparam_desc_t desc_1;
	strcpy(desc_1.mname,"net");
	desc_1.module = NET_MODULE;
	desc_1.get = net_get_func;
	desc_1.set = net_set_func;

	ret = ups_init_conf(desc_1,&defnet,sizeof(defnet));
	zlog_debug(zc_net,"ups_init_conf ret = %d\n",ret);

	/** test ups get */
	struct net_conf net_1;
	memset(&net_1,0,sizeof(net_1));
	sparam_info_t info_1;
	info_1.module = NET_MODULE;
	info_1.conf = &net_1;
	info_1.size = sizeof(struct net_conf);
	ret = ups_get_conf(&info_1,NULL,0);
	zlog_debug(zc_net,"ups_get_conf ret = %d\n",ret);

	/** test ups set */
	strcpy(net_1.ip,"10.0.0.15");
	strcpy(net_1.mask,"255.0.0.0");
	strcpy(net_1.gw,"10.0.0.1");
	strcpy(net_1.dns1,"10.0.0.1");
	strcpy(net_1.dns2,"10.0.0.1");
	net_1.port = 19200;

	info_1.module = NET_MODULE;
	info_1.conf = &net_1;
	info_1.size = sizeof(struct net_conf);
	info_1.persist = 1;
	ret = ups_set_conf(info_1,NULL,0);
	zlog_debug(zc_net,"ups_set_conf ret = %d\n",ret);

	ret = ups_exit_conf(desc_1);
	zlog_debug(zc_net,"ups_exit_conf ret = %d\n",ret);
	
	return (void*)0;
}

void *media_module_thread(void *arg)
{
	int ret = 0;
	zlog_category_t *zc_media = zlog_get_category("media");

	sparam_desc_t desc_1;
	strcpy(desc_1.mname,"media");
	desc_1.module = MEDIA_MODULE;
	desc_1.get = media_get_func;
	desc_1.set = media_set_func;

	ret = ups_init_conf(desc_1,&def_venc,sizeof(def_venc));
	zlog_debug(zc_media,"ups_init_conf ret = %d\n",ret);

	/** test ups get */
	struct media_conf media_1;
	memset(&media_1,0,sizeof(media_1));
	sparam_info_t info_1;
	info_1.module = MEDIA_MODULE;
	info_1.conf = &media_1;
	info_1.size = sizeof(struct media_conf);
	ret = ups_get_conf(&info_1,NULL,0);
	zlog_debug(zc_media,"ups_get_conf ret = %d\n",ret);

	/** test ups set */
    media_1.chn = 2;
    media_1.Stream = 1; 
    media_1.w = 640;
    media_1.h = 480; 
    media_1.FrameRate = 10;
    media_1.Standard = 1; 
    media_1.RateType = 1; 
    media_1.dwBitRate = 1024; 
    media_1.ImageQuality = 5;
    media_1.nMaxKeyInterval = 10; 

	info_1.module = MEDIA_MODULE;
	info_1.conf = &media_1;
	info_1.size = sizeof(struct media_conf);
	info_1.persist = 1;
	ret = ups_set_conf(info_1,NULL,0);
	zlog_debug(zc_media,"ups_set_conf ret = %d\n",ret);

	ret = ups_exit_conf(desc_1);
	zlog_debug(zc_media,"ups_exit_conf ret = %d\n",ret);
	
	return (void*)0;
}

int main(int argc, char *argv[])
{
	int ret = 0;
	int mask = 0;
	pthread_t net_tid,media_tid;

	printf(">>>start ups test demo...\n");

	if(zlog_init(UPS_LOG_CONF)<0)
	{
		printf("dzlog init failed!\n");
		exit(EXIT_FAILURE);
	}

	zlog_category_t *zc_demo = zlog_get_category("demo");

	if(ups_init_env("demo")<0)
	{
		zlog_error(zc_demo,"ups_init_env failed!\n");
		ret = -1;
	}
	else
	{
		if(pthread_create(&net_tid,NULL,net_module_thread,NULL)<0)
		{
			zlog_error(zc_demo,"create net_module_thread failed!\n");
			ret = -1;
		}
		mask |= 1;
		if(pthread_create(&media_tid,NULL,media_module_thread,NULL)<0)
		{
			zlog_error(zc_demo,"create media_module_thread failed!\n");
			ret = -1;
		}
		mask |= (1<<1);
	}

	if(mask & 1)
		pthread_join(net_tid, NULL);
	if(mask & 2)
		pthread_join(media_tid, NULL);

	if(!ret)
		ups_exit_env();
	zlog_fini();
	printf("<<<end ups test demo.\n");

	return ret;
}

